﻿using System;
using System.Net;
using System.Threading.Tasks;
namespace Chaos
{
    public class TransmitPower
    {
        public const int DefaultWebRequestTimeout = 10000;
        public const int DefaultWebRequestReadWriteTimeout = 10000;

        private async Task<IChaosResponsePackage> DoTransmit(Uri uriRequest, WebHeaderCollection webHeaderCollection, byte[] buffer, int timeout = DefaultWebRequestTimeout, int readWriteTimeout = DefaultWebRequestReadWriteTimeout)
        {
            var httpRequest = WebRequest.CreateHttp(uriRequest);
            httpRequest.Headers = webHeaderCollection;
            httpRequest.Timeout = timeout;
            httpRequest.ReadWriteTimeout = DefaultWebRequestReadWriteTimeout;
            var chaosResponsePackage = new ChaosResponsePackage();
            using (var requestStream = await httpRequest.GetRequestStreamAsync())
            {
                await requestStream.WriteAsync(buffer, 0, buffer.Length);
                using (var httpResponse = await httpRequest.GetResponseAsync())
                {
                    chaosResponsePackage.Headers = httpResponse.Headers;
                    using (var responseStream = httpResponse.GetResponseStream())
                    {
                        chaosResponsePackage.Buffer = new byte[responseStream.Length];

                        await responseStream.ReadAsync(chaosResponsePackage.Buffer, 0, chaosResponsePackage.Buffer.Length);
                        responseStream.Close();
                    }
                }
            }
            return chaosResponsePackage;
        }
    }
}
    